package com.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageResult;
import com.mall.product.dao.BrandDao;
import com.mall.product.entity.BrandEntity;
import com.mall.product.service.BrandService;
import org.springframework.stereotype.Service;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {


    @Override
    public PageResult<BrandEntity> page(int current, int size, Long brandId, String name, String firstLetter) {
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();

        if (brandId != null) {
            queryWrapper.eq("brand_id", brandId);
        }

        if (firstLetter != null && !"".equals(firstLetter)) {
            queryWrapper.or().eq("first_letter", firstLetter);
        }
        if (name != null && !"".equals(name)) {
//            queryWrapper.eq("name", name);
            //名字模糊查询
//            queryWrapper.or().like("name", name);
            queryWrapper.and(t -> {
                t.like("name", name);
            });
        }
        Page<BrandEntity> page = new Page<>(current, size);
        Page<BrandEntity> brandEntitypage = baseMapper.selectPage(page, queryWrapper);

        return new PageResult<>(brandEntitypage.getRecords(), brandEntitypage.getTotal());
    }
}